package com.dimples.controller.system;

import com.dimples.common.annotation.OpsLog;
import com.dimples.common.enums.OpsLogTypeEnum;
import com.dimples.common.page.metadata.Page;
import com.dimples.common.transport.QueryRequest;
import com.dimples.common.transport.Result;
import com.dimples.controller.view.BaseWebController;
import com.dimples.system.entity.SysDept;
import com.dimples.system.service.SysDeptService;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;

import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

import javax.validation.Valid;
import javax.validation.constraints.NotBlank;
import javax.validation.constraints.NotNull;

import cn.hutool.core.lang.tree.Tree;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;

/**
 * @author zhongyj <1126834403@qq.com><br/>
 * @date 2021/6/26
 */
@Slf4j
@Api(tags = "部门管理")
@Validated
@RestController
@RequestMapping(value = BaseWebController.API_SYSTEM_PREFIX)
@RequiredArgsConstructor
public class SysDeptController {

    private final SysDeptService sysDeptService;

    @ApiOperation(value = "获取部门列表")
    @ApiOperationSupport(order = 1)
    @OpsLog(value = "获取部门列表", type = OpsLogTypeEnum.SELECT)
    @GetMapping("dept")
    @PreAuthorize("@auth.hasPerm('dept:view')")
    public Result<SysDept> findDept(SysDept sysDept, QueryRequest queryRequest) {
        Page<SysDept> result = this.sysDeptService.findDept(sysDept, queryRequest);
        return Result.success(result);
    }

    @ApiOperation(value = "获取部门树")
    @ApiOperationSupport(order = 3)
    @OpsLog(value = "获取部门树", type = OpsLogTypeEnum.SELECT)
    @GetMapping("dept/tree")
    @PreAuthorize("@auth.hasPerm('dept:view')")
    public Result<Tree<Long>> findDeptTree() {
        List<Tree<Long>> result = this.sysDeptService.findDeptTree();
        return Result.success(result);
    }

    @ApiOperation(value = "新增部门")
    @ApiOperationSupport(order = 3)
    @OpsLog(value = "新增部门", type = OpsLogTypeEnum.ADD)
    @PostMapping("dept")
    @PreAuthorize("@auth.hasPerm('dept:add')")
    public Result<SysDept> saveDept(@Valid @RequestBody SysDept sysDept) {
        this.sysDeptService.saveDept(sysDept);
        return Result.success();
    }


    @ApiOperation(value = "编辑部门状态")
    @ApiOperationSupport(order = 5)
    @OpsLog(value = "编辑部门状态", type = OpsLogTypeEnum.UPDATE)
    @PutMapping("dept/status")
    @PreAuthorize("@auth.hasPerm('dept:status:edit')")
    public Result<SysDept> updateDeptStatus(@RequestBody SysDept sysDept) {
        this.sysDeptService.updateDept(sysDept);
        return Result.success();
    }

    @ApiOperation(value = "编辑部门")
    @ApiOperationSupport(order = 7)
    @OpsLog(value = "编辑部门", type = OpsLogTypeEnum.UPDATE)
    @PutMapping("dept")
    @PreAuthorize("@auth.hasPerm('dept:edit')")
    public Result<SysDept> updateDept(@Valid @RequestBody SysDept sysDept) {
        this.sysDeptService.updateDept(sysDept);
        return Result.success();
    }

    @ApiOperation(value = "删除部门")
    @ApiOperationSupport(order = 9)
    @OpsLog(value = "删除部门", type = OpsLogTypeEnum.DELETE)
    @DeleteMapping("dept/{deptId}")
    @PreAuthorize("@auth.hasPerm('dept:delete')")
    public Result<SysDept> deleteDept(@PathVariable @NotNull(message = "不能为空") Long deptId) {
        this.sysDeptService.deleteDept(deptId);
        return Result.success();
    }

    @ApiOperation(value = "批量删除部门")
    @ApiOperationSupport(order = 11)
    @OpsLog(value = "批量删除部门", type = OpsLogTypeEnum.DELETE)
    @DeleteMapping("dept/batch/{deptIds}")
    @PreAuthorize("@auth.hasPerm('dept:delete')")
    public Result<SysDept> deleteDepts(@PathVariable @NotBlank(message = "不能为空") String deptIds) {
        this.sysDeptService.deleteDepts(deptIds);
        return Result.success();
    }

}
